﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="/Scripts/szjt-allScripts.js" type="text/javascript"></script>
</head>
<body>
    <table id="codeGrid"></table>
    <script type="text/javascript">
        var _codeCategory = utility.page.getQueryString("category");
        var _isShowCode = (utility.page.getQueryString("type") == "code");
        var _isShowStatus = (utility.page.getQueryString("status") == "yes");
        var _colWidths = getColWidths(utility.page.getQueryString("widths"));
        var _fixedCodes = getFixedCodes(utility.page.getQueryString("fixed"));
        var _lastEditIndex;
        var _isInEditing = false;

        function getColWidths(optionList) {
            var colWidths = { CodeName: 150, CodeValue: 200 };
            if (optionList) {
                var colOptions = optionList.split(",");
                var i;
                if (_isShowCode) {
                    for (i in colOptions) {
                        colWidths[i] = colOptions[i];
                    }
                } else {
                    colWidths.CodeValue = colOptions[0];
                }
            }
            return colWidths;
        }

        function getFixedCodes(codesString) {
            if (codesString) {
                return codesString += ","; //  codesString.split(",");
            } else {
                return undefined;
            }
        }

        function setEditingStatus(inEditing) {
            _isInEditing = inEditing;
            var isSelected = ($("#codeGrid").datagrid('getSelections').length > 0);
            $("#btnAdd").linkbutton({ disabled: _isInEditing });
            $("#btnEdit").linkbutton({ disabled: _isInEditing || !isSelected });
            $("#btnDelete").linkbutton({ disabled: _isInEditing || !isSelected });
            $("#btnAccept").linkbutton({ disabled: !_isInEditing });
            $("#btnReject").linkbutton({ disabled: !_isInEditing });
            $("#btnSave").linkbutton({ disabled: _isInEditing });
            //$("#btnReset").linkbutton({ disabled: !_isInEditing });
        }

        function loadData() {
            var codeSearchTemp = SysConstant.ExtComponent.System.CodeTable.CodeTableList.Template.replace("{0}", _codeCategory).replace("{1}", "").replace("{2}", false);
            var codeDataText = dataService.transformData(SysConstant.TaskGuid.ExtComponent, codeSearchTemp);
            $("#codeGrid").detailgrid("loadXmlString", { xml: codeDataText, totalNode: "", dataNode: "CodeInfo" });
            setEditingStatus(false);
        }

        function saveData(rowIndex) {
            var codeTemplate = utility.xml.convertXmlStringToDoc(SysConstant.ExtComponent.System.CodeTable.CodeTableInfo.Template);
            var rowData = $("#codeGrid").datagrid("getSelected");
            if (!_isShowCode)
                rowData.CodeName = rowData.CodeValue;
            var dataGuid = rowData.CodeGuid;
            var codeName = rowData.CodeName;
            for (var x in rowData) {
                codeTemplate.find(x).text(rowData[x]);
            }
            codeTemplate.find("CodeCategory").text(_codeCategory);
            codeTemplate.find("CodeStatus").text("T");
            var xmlData = utility.xml.convertXmlDocToString(codeTemplate);
            var result = dataService.setData(SysConstant.TaskGuid.ExtComponent, dataGuid, SysConstant.ExtComponent.System.CodeTable.CodeTableInfo.DataType, xmlData);
            utility.message.promptOperationResult("保存", _codeCategory + " - " + codeName, (result == 0));
            return result;
        }

        function deleteData(rowData) {
            //var rowData = $("#codeGrid").datagrid("getSelected");
            var dataGuid = rowData.CodeGuid;

            var result = dataService.delDataList(SysConstant.TaskGuid.ExtComponent, dataGuid, SysConstant.ExtComponent.System.CodeTable.CodeTableInfo.DataType);
            utility.message.promptOperationResult("删除", _codeCategory, (result == 0));
            return result;
        }

        $(document).ready(function () {
            var codeNameTitle = utility.page.getQueryString("nameTitle") || (_codeCategory + "代码");
            var codeValueTitle = utility.page.getQueryString("valueTitle") || (_codeCategory + (_isShowCode ? "对应值" : ""));

            $("#codeGrid").datagrid({
                singleSelect: true,
                columns: [[
                    { field: 'CodeGuid', title: 'GUID', hidden: true },
                    { field: 'CodeOrder', title: '序号', width: 50, editor: { type: 'numberbox', options: { precision: 0, required: true}} },
                //                    (_isShowCode ?
                //                        { field: 'CodeName', title: codeNameTitle, width: _colWidths.CodeName, editor: { type: "text", options: { required: true}}} :
                //                        { field: 'CodeName', title: codeNameTitle, hidden: true }),
                    {field: 'CodeName', title: codeNameTitle, width: _colWidths.CodeName, hidden: !_isShowCode, editor: { type: "text", options: { required: true}} },
                    { field: 'CodeValue', title: codeValueTitle, width: _colWidths.CodeValue, editor: { type: "validatebox", options: { required: true}} },
                    { field: 'CodeStatus', title: '状态', width: _colWidths.CodeValue, hidden: !_isShowStatus,
                        formatter: function (value, rowData, rowIndex) { return (value == "T" ? "启用" : "停用"); },
                        editor: { type: "checkbox", options: { on: "T", off: "F"} }
                    }
                ]],
                //                rowStyler: function (index, rowData) {
                //                    if (rowData.CodeStatus == "F") {
                //                        return 'background-color:#6293BB;color:#333333';
                //                    }
                //                },
                toolbar: [{
                    id: "btnAdd",
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        $('#codeGrid').datagrid('endEdit', _lastEditIndex);
                        _lastEditIndex = $("#codeGrid").datagrid('getRows').length;
                        $('#codeGrid').datagrid('appendRow', {
                            CodeGuid: dataService.getNewGuid(),
                            CodeOrder: _lastEditIndex + 1,
                            CodeName: '',
                            CodeValue: '',
                            CodeStatus: 'T'
                        });
                        $("#codeGrid").datagrid('selectRow', _lastEditIndex);
                        $("#codeGrid").datagrid('beginEdit', _lastEditIndex);
                        setEditingStatus(true);
                    }
                }, {
                    id: "btnEdit",
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        if (_lastEditIndex) {
                            $("#codeGrid").datagrid('beginEdit', _lastEditIndex);
                            setEditingStatus(true);
                        }
                    }
                }, {
                    id: "btnDelete",
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var row = $("#codeGrid").datagrid('getSelected');
                        if (row) {
                            var codeName = row.CodeName;
                            if (_fixedCodes != undefined && _fixedCodes.indexOf(codeName) >= 0) {
                                $.messager.alert("特殊值不能删除", "【" + codeName + "】属系统特殊值，不能删除。", "info");
                            } else {
                                $.messager.confirm('删除确认', '确认要删除【' + row.CodeName + '】吗？', function (r) {
                                    if (r) {
                                        deleteData(row);
                                        var index = $("#codeGrid").datagrid('getRowIndex', row);
                                        $("#codeGrid").datagrid('deleteRow', index);
                                    }
                                });
                            }
                        }
                    }
                }, '-', {
                    id: "btnAccept",
                    text: '完成',
                    iconCls: 'icon-ok',
                    handler: function () {
                        if ($("#codeGrid").datagrid("validateRow", _lastEditIndex)) {
                            var codeEditor = $("#codeGrid").datagrid("getEditor", { index: _lastEditIndex, field: (_isShowCode ? "CodeName" : "CodeValue") });
                            var oldCode = codeEditor.oldHtml;
                            var newCode = codeEditor.actions.getValue(codeEditor.target);
                            if (_fixedCodes != undefined && oldCode != "" && _fixedCodes.indexOf(oldCode+",") >= 0 && oldCode != newCode) {
                                $.messager.alert("特殊值不能修改", "【" + oldCode + "】属系统特殊值，只能修改序号，但不能修改码值。", "info");
                            } else {
                                var row = $("#codeGrid").datagrid("getSelected");
                                $("#codeGrid").datagrid('acceptChanges');
                                if (saveData(_lastEditIndex) == 0) {
                                    loadData();
                                }
                            }
                        }
                    }
                }, {
                    id: "btnReject",
                    text: '取消',
                    iconCls: 'icon-undo',
                    handler: function () {
                        $("#codeGrid").datagrid('rejectChanges');
                        setEditingStatus(false);
                    }
                }, '-', {
                    id: "btnReset",
                    text: '刷新',
                    iconCls: 'icon-reload',
                    handler: function () {
                        loadData();
                    }
                }],
                onBeforeLoad: function () {
                    $(this).datagrid('rejectChanges');
                },
                onClickRow: function (rowIndex) {
                    _lastEditIndex = rowIndex;
                    setEditingStatus(false);
                },
                onDblClickRow: function (rowIndex) {
                    if (_lastEditIndex != rowIndex) {
                        $("#codeGrid").datagrid('endEdit', _lastEditIndex);
                        $("#codeGrid").datagrid('beginEdit', rowIndex);
                        setEditingStatus(true);
                    }
                    _lastEditIndex = rowIndex;
                },
                onBeforeEdit: function (rowIndex, rowData) {

                }
            });
            loadData();
        });
    </script>
</body>
</html>